﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Web;

namespace SalesSys.Comm.Utility
{
    public class PagerUrlExt
    {
        #region 分页相关
        /// <summary>
        /// 判断URL参数字符传中是否包含特殊字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool CheckSpecialChar(string str)
        {
            var regex = new System.Text.RegularExpressions.Regex(@"[~'!<>@#$%^&*()+=:]+");
            return regex.IsMatch(str);
        }
        /// <summary>
        /// 条件查询分类
        /// </summary>
        public enum ConditionsType
        {
            Sort, Type, Lec, MCard, All,
            /// <summary>
            /// 二级分类，与一级分类Type，用同一个标签type
            /// </summary>
            SType,
            /// <summary>
            /// 机构
            /// </summary>
            Ptr
        }
        /// <summary>
        /// 排序方法
        /// </summary>
        public enum Sorttype
        {
            Asc, Desc
        }

        #region 参数处理
        /// <summary>
        /// 处理参数
        /// </summary>
        /// <param name="ct"></param>
        /// <param name="querystring"></param>
        /// <returns></returns>
        public static string GetUrlPar(ConditionsType ct, NameValueCollection querystring)
        {
            var par = new StringBuilder();
            try
            {

                // url.Append(Request.Url.AbsolutePath + "?sort={0}");
                NameValueCollection collection = querystring;
                
                var keys = collection.AllKeys;

                switch (ct)
                {
                    case ConditionsType.Type:
                        for (int i = 0; i < keys.Length; i++)
                        {
                            //后将条件中的&&keys[i].ToLower()!="g"去掉了
                            if (keys[i].ToLower() != "page" && keys[i].ToLower() != "type")
                                par.AppendFormat("&{0}={1}", keys[i], HttpUtility.UrlEncode(collection[keys[i]]));
                        }
                        break;
                    case ConditionsType.Lec:
                        for (int i = 0; i < keys.Length; i++)
                        {
                            if (keys[i].ToLower() != "page" && keys[i].ToLower() != "lec")
                                par.AppendFormat("&{0}={1}", keys[i], HttpUtility.UrlEncode(collection[keys[i]]));
                        }
                        break;
                    case ConditionsType.Sort:
                        for (int i = 0; i < keys.Length; i++)
                        {
                            if (keys[i].ToLower() != "page" && keys[i].ToLower() != "sort")
                                par.AppendFormat("&{0}={1}", keys[i], HttpUtility.UrlEncode(collection[keys[i]]));
                        }
                        break;
                    case ConditionsType.MCard:
                        for (int i = 0; i < keys.Length; i++)
                        {
                            if (keys[i].ToLower() != "page" && keys[i].ToLower() != "mcard")
                                par.AppendFormat("&{0}={1}", keys[i], HttpUtility.UrlEncode(collection[keys[i]]));
                        }
                        break;
                    case ConditionsType.Ptr:
                        for (int i = 0; i < keys.Length; i++)
                        {
                            if (keys[i].ToLower() != "page" && keys[i].ToLower() != "ptr")
                                par.AppendFormat("&{0}={1}", keys[i], HttpUtility.UrlEncode(collection[keys[i]]));
                        }
                        break;
                    case ConditionsType.All:
                        for (int i = 0; i < keys.Length; i++)
                        {
                            //if (keys[i]!=null)
                            //{
                            par.AppendFormat("&{0}={1}", keys[i], HttpUtility.UrlEncode(collection[keys[i]]));
                            //}
                        }
                        break;
                }
            }
            catch (Exception)
            {
                par = new StringBuilder();
            }

            return par.ToString();
        }
        /// <summary>
        /// 获取url中的参数字典，除nocontiaonkey之外的
        /// </summary>
        /// <param name="querystring">querystring</param>
        /// <param name="nocontainkey">不包含nocontionkey的参数</param>
        /// <returns></returns>
        public static SortedDictionary<string, string> GetUrlPar(NameValueCollection querystring, string nocontainkey)
        {
            SortedDictionary<string, string> sPara = new SortedDictionary<string, string>();
            NameValueCollection coll = querystring;
            String[] requestItem = coll.AllKeys;

            for (int i = 0; i < requestItem.Length; i++)
            {
                if (requestItem[i] != nocontainkey)
                {
                    sPara.Add(requestItem[i], querystring[requestItem[i]]);
                }
            }
            return sPara;
        }


        #endregion
        #endregion
        #region 验证图片格式
        public static bool ValidateImgType(string imgName)
        {
            var imgType = new string[] { "gif", "jpg", "png" };

            var i = 0;
            var blean = false;
            var message = string.Empty;

            //判断是否为Image类型文件
            while (i < imgType.Length)
            {
                if (imgName.Equals(imgType[i]))
                {
                    blean = true;
                    break;
                }
                if (i == (imgType.Length - 1))
                {
                    break;
                }
                i++;
            }
            return blean;
        }
        #endregion
    }
}
